package com.seio.edc.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.model.system.ModelConstant;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.DeviceAir;
import com.nova.edc.model.system.DeviceType;
import com.nova.edc.model.system.Login;
import com.nova.edc.service.system.DeviceAirService;
import com.nova.edc.service.system.DeviceTypeService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;

@Controller
@RequestMapping(value = "/systemDeviceAir")
public class DeviceAirContoller {
    @Autowired
    private DeviceAirService deviceAirService;
    @Autowired
    private DeviceTypeService deviceTypeService;
    private final int PAGE_SIZE = 10;
    static Logger log = Logger.getLogger(DeviceAirContoller.class);

    @InitBinder("leEntity")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("leEntity.");
    }

    /**
     * 灯具能耗表主页面
     */
    @RequestMapping(value = "deviceAirList.shtml")
    public ModelAndView deviceAirList(HttpServletRequest request,
                                      HttpSession session, ModelMap modelMap, Integer pageIndex,
                                      DeviceAir deviceAir) {
        // 所有灯具
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        PageList<DeviceAir> pageList = deviceAirService.findPageDeviceAir(
                deviceAir, page, "AddDate", "desc");

//        PageList<DeviceAir> pageList = null;
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        // 数据列表
        modelMap.put("list", pageList.getDatas());

        return new ModelAndView("system/deviceAirList", modelMap);
    }

    /**
     * 添加初始化
     *
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "deviceAirEdit.shtml")
    public ModelAndView deviceAirEdit(HttpServletRequest request,
                                      ModelMap modelMap, Integer id) {
        if (id != null && id != 0) {
            DeviceAir deviceAir = deviceAirService.getById(id);
            modelMap.put("deviceAir", deviceAir);
            modelMap.put("editFlag", "update");
        } else {
            modelMap.put("editFlag", "add");
        }
        return new ModelAndView("system/deviceAirEdit", modelMap);
    }

    /**
     * 添加login
     */
    @RequestMapping(value = "save.shtml")
    public ModelAndView save(@ModelAttribute("deviceAir") DeviceAir deviceAir,
                             HttpSession session, HttpServletRequest request, ModelMap modelMap) {
        Login sessionLogin = (Login) session.getAttribute("sessionLogin");
        DeviceType deviceType = deviceTypeService
                .getByName(ModelConstant.DEVICE_AIR);
        deviceAir.setDevicetypeid(deviceType != null ? deviceType.getId() : 0);
        if (deviceAir.getId() != null && deviceAir.getId() != 0) {
            deviceAir.setModifyby(sessionLogin.getUsername());
            deviceAir.setModifydate(DateUtils.getCurrentDate());
            deviceAir.setModifystamp(DateUtils.getCurrentDate());
            deviceAirService.update(deviceAir);
        } else {
            deviceAir.setScrapped(0);
            deviceAir.setAdddate(DateUtils.getCurrentDate());
            deviceAir.setAddby(sessionLogin.getUsername());
            deviceAirService.insert(deviceAir);
        }

        return deviceAirList(request, session, modelMap, null, new DeviceAir());

    }

    @RequestMapping(value = "delete.shtml")
    public ModelAndView delete(int id, HttpServletRequest request,
                               HttpSession session, ModelMap modelMap) {
        deviceAirService.deleteById(id);
        return deviceAirList(request, session, modelMap, null, new DeviceAir());
    }

    @RequestMapping(value = "checkAircode.shtml")
    @ResponseBody
    public Map<String, Object> checkAircode(HttpServletRequest request,
                                            String airCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("existFlag", deviceAirService.isExistCode(airCode));
        return map;
    }
}
